<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/7/27 0027
 * Time: 11:25
 */

namespace app\api\controller\v1\agent;


use app\api\controller\v1\Agent;
use app\currency\service\AgentService;
use app\currency\service\CitiesService;
use think\Db;

class WorkLogs extends Agent
{
    /**
     * 团队做工记录
     */
    public function index()
    {
        $ids = AgentService::getTeamIds($this->aid);
        $where = [['cid','in',$ids],['status','eq','4']];
        $list = $this->_query('CleanOrder')->where($where)->order('create_at desc')->page(true,false,false,10);
        $this->success('获取成功！',$list);
    }
    protected function _index_page_filter(&$data)
    {
        $cids = array_unique(array_column($data,'cid'));
        $cleanerList = Db::name('Cleaner')->whereIn('id',$cids)->select();
        foreach ($data as &$datum) {
            $datum['blacklist_num'] = Db::name('MemberBlacklist')->where('cid',$datum['cid'])->count();
            $datum['address_txt'] = CitiesService::getAddressText($datum['cities_id'],'string');
            $datum['cleaner'] = [];
            foreach ($cleanerList as $cleaner) if ($cleaner['id'] === $datum['cid']) {
                $datum['cleaner'] = $cleaner;
            }
            $datum['rebate_amount'] = Db::name('AgentRebate')->where([
                ['modal','eq','service'],
                ['agent_id','eq',$this->aid],
                ['cid','eq',$datum['cid']],
                ['order_no','eq',$datum['order_no']],
            ])->value('amount') ?: 0;
        }
    }
}